﻿using System;
using System.Linq;
using CommonBehaviors.Actions;
using FarmerJoe.ClassSettings;
using FarmerJoe.Helpers;
using FarmerJoe.Settings;
using Styx;
using Styx.CommonBot;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;
using FarmerJoe.Helpers.Units;

namespace FarmerJoe.Classes
{
    public static partial class Druid
    {
        public static Composite CombatBehavior_Restoration
        {
            get { return Behaviour_Restoration(ClassParser.CCControlledMovement); }
        }

        private static WoWUnit _tank = null;
        private static WoWUnit Tank
        {
            get
            {
                if (Timers.SpellOkToCast("Tank Check",15000) || _tank == null)
                    _tank = Group.Tanks.FirstOrDefault();
                
                return _tank;
            }
        }

        private static WoWUnit _healTarget = null;
        private static WoWUnit HealTarget
        {
            get
            {
                if (Timers.SpellOkToCast("Heal Target Check",350) || _healTarget == null)
                    _healTarget = Healing.Instance.FirstUnit;
                
                return _healTarget;
            }
        }

        private static bool InCombat
        {
            get
            {
                return Me.Combat;
            }
        }

        public static Composite Behaviour_Restoration(bool moveToTarget)
        {
            Healing.NeedHealTargeting = true;

            return new PrioritySelector(
                Spells.WaitForCast(),
                AllFormsHeal(),

                // Trying to fix a small issue
                new Decorator(ret=> Me.IsCasting || SpellManager.GlobalCooldown || (Me.HasAura("Food") && Me.HealthPercent < 80) || (Me.HasAura("Drink") && Me.ManaPercent < 80) ,new ActionAlwaysSucceed()),

                // Move to line of sight on the target
                new Decorator(ret => moveToTarget, Movement.MoveToLOS(ret => (WoWUnit) ret)),

                // Res players out of combat
                new Decorator(ret => FpswareSettings.Instance.Druid.ResPlayers.Contains("always") && !Me.Combat && !Me.HasAura("Drink") && !Me.HasAura("Food") && Me.CurrentMana > Spells.PowerCost("Revive"), Spells.Resurrect("Revive")),

                Spells.Cast("Force of Nature", ret=> FpswareSettings.Instance.Druid.ForceOfNatureResto.Contains("always") && InCombat && HealTarget != null && HealTarget.HealthPercent < 95),

                // Tank
                new Decorator(ret => Tank != null,
                    new PrioritySelector(

                    Spells.Heal("Lifebloom", ret => Tank, ret => FpswareSettings.Instance.Druid.LifebloomTank.Contains("always") && InCombat && Tank.HealthPercent > 60 && (!Tank.HasAura("Lifebloom") || Tank.Auras["Lifebloom"].StackCount < 3 || Tank.Auras["Lifebloom"].TimeLeft <= TimeSpan.FromSeconds(3))),
                    Spells.Heal("Rebirth", ret => Tank, ret => InCombat && FpswareSettings.Instance.Druid.BattleResTank.Contains("always") && Tank.IsDead)
                    )),

                // Party AoE
                Spells.Heal("Wild Growth", ret => HealTarget, ret => FpswareSettings.Instance.Druid.WildGrowth.Contains("always") && HealTarget != null && StyxWoW.Me.GroupInfo.IsInParty && AddsAndOthers.NearbyFriendlyPlayers.Count(p => p.IsAlive && p.HealthPercent <= FpswareSettings.Instance.Druid.WildGrowthHealth && p.Location.DistanceSqr((HealTarget).Location) <= 30 * 30) >= Convert.ToInt32(FpswareSettings.Instance.Druid.WildGrowthCount)),
                Spells.Heal("Tranquility", ret => FpswareSettings.Instance.Druid.Tranquility.Contains("always") && StyxWoW.Me.GroupInfo.IsInParty && AddsAndOthers.NearbyFriendlyPlayers.Count( p => p.IsAlive && p.HealthPercent <= FpswareSettings.Instance.Druid.TranquilityHealth && p.Distance2DSqr <= 40*40) >= Convert.ToInt32(FpswareSettings.Instance.Druid.TranquilityCount)),

                // Tank
                new Decorator(ret => Tank != null && Tank.HealthPercent < 98,
                    new PrioritySelector(
                    Spells.Heal("Swiftmend", ret => Tank, ret => Tank.HealthPercent <= FpswareSettings.Instance.Druid.SwiftmendTankHealth && (Tank.HasMyAura("Rejuvenation") || Tank.HasMyAura("Regrowth"))),
                    Spells.Heal("Rejuvenation", ret => Tank, ret => !Tank.HasMyAura("Rejuvenation") && Tank.HealthPercent <= FpswareSettings.Instance.Druid.RejuvenationTankHealth),
                    Spells.Heal("Healing Touch", ret => Tank, ret => Tank.HealthPercent <= FpswareSettings.Instance.Druid.HealingTouchTankHealth),
                    Spells.Heal("Regrowth", ret => Tank, ret => !Tank.HasMyAura("Regrowth") && Tank.HealthPercent <= FpswareSettings.Instance.Druid.RegrowthTankHealth)
                    )),
                

                // Party
                new Decorator(ret => HealTarget != null,// && HealTarget != Tank,
                    new PrioritySelector(

                    Spells.Heal("Swiftmend", ret => HealTarget, ret => HealTarget.HealthPercent <= FpswareSettings.Instance.Druid.SwiftmendHealth && (HealTarget.HasMyAura("Rejuvenation") || HealTarget.HasMyAura("Regrowth"))),
                    Spells.Heal("Rejuvenation", ret => HealTarget, ret => !HealTarget.HasMyAura("Rejuvenation") && HealTarget.HealthPercent <= FpswareSettings.Instance.Druid.RejuvenationHealth),
                    Spells.Heal("Healing Touch", ret => HealTarget, ret => HealTarget.HealthPercent <= FpswareSettings.Instance.Druid.HealingTouchHealth),
                    Spells.Heal("Regrowth", ret => HealTarget, ret => !HealTarget.HasMyAura("Regrowth") && HealTarget.HealthPercent <= FpswareSettings.Instance.Druid.RegrowthHealth),
                    Spells.Heal("Nourish", ret => HealTarget, ret => HealTarget.HealthPercent <= FpswareSettings.Instance.Druid.NourishHealth) //&& ((HealTarget.HasAura("Rejuvenation") || HealTarget.HasAura("Regrowth") || HealTarget.HasAura("Lifebloom") || HealTarget.HasAura("Wild Growth"))))
                    )),


                // Basic combat
                new Decorator( ret => InCombat && StyxWoW.Me.GotTarget && AddsAndOthers.NearbyFriendlyPlayers.Count(u => u.IsInMyPartyOrRaid) == 0,
                    new PrioritySelector(
                        Movement.MoveToLOS(), 
                        Movement.FaceTarget(), 
                        Spells.Cast("Wrath"),
                        Movement.MoveToTarget(true, 35f))),


                new Decorator(ret => moveToTarget, Movement.MoveToTarget(true, 35f, ret => HealTarget))
                );
        }

    }
}
